﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestMain : MonoBehaviour {

    GameObject gameObjBall = null;

    //GameObject gameObjBox = null;

    GameObject gameObjUI = null;

    public Mesh lineMesh;

    Text txtSpeed = null;

    Slider sliderPower = null;

    Button btnShoot = null;

    InputField ifDirX = null;
    InputField ifDirZ = null;

    // Use this for initialization
    void Start () {
        Object objPrefab = Resources.Load("Ball");
        //Object objPrefabBox = Resources.Load("Box");

        gameObjBall = (GameObject)GameObject.Instantiate(objPrefab,
            new Vector3(100 + Random.Range(-0.2f, 0.2f), 5f, 120 + Random.Range(1f, 5f)),
            Quaternion.identity);

        Camera.main.GetComponent<CameraFollow>().target = gameObjBall.transform;

        gameObjBall.GetComponent<Rigidbody>().mass = 1;

        //gameObjBox = (GameObject)GameObject.Instantiate(objPrefabBox,
        //    new Vector3(23f + Random.Range(-0.2f, 0.2f), 5f, 23f + Random.Range(-0.2f, 0.2f)),
        //    Quaternion.identity);

        gameObjUI = GameObject.Find("Canvas");
        Button btn = gameObjUI.transform.Find("Button").GetComponent<Button>();
        btn.onClick.AddListener(OnClick);

        txtSpeed = gameObjUI.transform.Find("Text").GetComponent<Text>();

        ifDirX = gameObjUI.transform.Find("IfDirX").GetComponent<InputField>();
        ifDirX.text = "200";
        ifDirZ = gameObjUI.transform.Find("IfDirZ").GetComponent<InputField>();
        ifDirZ.text = "200";

        sliderPower = gameObjUI.transform.Find("SliderPower").GetComponent<Slider>();
        sliderPower.onValueChanged.AddListener(onValueChanged);
        sliderPower.value = 0.5f;

        btnShoot = gameObjUI.transform.Find("BtnShoot").GetComponent<Button>();
        btnShoot.onClick.AddListener(OnClickShoot);
    }

    private void OnClickShoot()
    {
        float value = sliderPower.value;
        sliderPower.value = 0.5f;

        Vector3 v3 = gameObjBall.transform.position - Camera.main.transform.position;

        Debug.Log("v3:" + v3);

        v3 = v3.normalized;
        v3.y = 0;

        gameObjBall.GetComponent<Rigidbody>().AddForce(v3 * value * 2000f);
    }

    private void onValueChanged(float value)
    {
        
    }

    private void OnClick()
    {
        //gameObjBall.GetComponent<Rigidbody>().velocity = new Vector3(2, 0, 0);
        //gameObjBox.GetComponent<Rigidbody>().velocity = new Vector3(2, 0, 0);

        float fDirX = float.Parse(ifDirX.text);
        float fDirZ = float.Parse(ifDirZ.text);
        Debug.Log("fDirX:" + fDirX + ",fDirY:" + fDirZ);

        gameObjBall.GetComponent<Rigidbody>().AddForce(new Vector3(fDirX, 0, fDirZ));
        //gameObjBox.GetComponent<Rigidbody>().AddForce(new Vector3(fDirX, fDirY, 0));
    }

    // Update is called once per frame
    void Update () {

    }

    void LateUpdate()
    {
        float upY = 7;
        Vector3 targetPosition = gameObjBall.transform.position;
        targetPosition.y = upY;
        Vector3 cameraPosition = Camera.main.transform.position;
        cameraPosition.y = upY;

        // 方案2：摄像机固定跟随(球移动时)
        if (gameObjBall.GetComponent<Rigidbody>().velocity.magnitude > 0.3)
        {

            Vector3[] positions = new Vector3[2];
            gameObjBall.GetComponent<LineRenderer>().SetPositions(positions);

            Vector3 v3Dir = cameraPosition - targetPosition;
            targetPosition += v3Dir.normalized * 10;
            Camera.main.transform.position = targetPosition;

            // 相机永远朝向球
            Camera.main.transform.LookAt(gameObjBall.transform.position);
        }
        else
        {
            // 绘制射线
            Vector3 cPos = Camera.main.transform.position;
            cPos.y = gameObjBall.transform.position.y;
            Vector3 v3Dir = gameObjBall.transform.position - cPos;

            Vector3[] positions = new Vector3[2];
            positions[0] = gameObjBall.transform.position;
            positions[1] = gameObjBall.transform.position + v3Dir.normalized * 30;

            gameObjBall.GetComponent<LineRenderer>().SetPositions(positions);
        }

        Debug.DrawLine(gameObjBall.transform.position, targetPosition, Color.red);

        Debug.DrawLine(cameraPosition, targetPosition, Color.red);
        
    }

    void OnGUI()
    {
        txtSpeed.text = "ball speed:" + gameObjBall.GetComponent<Rigidbody>().velocity;
    }

    public void OnDrag(Vector2 dir, float value)
    {

    }

    public void OnEndDrag(Vector2 dir, float value)
    {
        Vector3 v3 = gameObjBall.transform.position - Camera.main.transform.position;

        Debug.Log("v3:" + v3);

        v3 = v3.normalized;
        v3.y = 0;


        gameObjBall.GetComponent<Rigidbody>().AddForce(v3 * value * 1000f);
    }
}
